/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.ui.popupmenus;

import java.util.ArrayList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.softao.ming.gde.internal.core.natures.Gde;
import com.softao.ming.gde.internal.core.natures.GdeProject;
import com.softao.ming.gde.internal.ui.GdeUI;

/**
 * 
 */
public class EnableGdeSupportAction implements IObjectActionDelegate {

	private ISelection mSelection;
	
	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		
	}

	@Override
	public void run(IAction action) {
		IProject[] unconverted = getUnconvertedProjects();
		if (unconverted.length == 0) {
			MessageDialog.openInformation(this.getDisplay().getActiveShell(), "Find Projects to Convert", "There are no projects to convert.");
			return;
		}
		
		int error = 0;
		
		if (mSelection instanceof IStructuredSelection) {
			Object[] elems = ((IStructuredSelection) mSelection).toArray();
			for (int i = 0; i < elems.length; i++) {
				Object elem = elems[i];
				IProject project = null;

				if (elem instanceof IFile) {
					IFile file = (IFile) elem;
					project = file.getProject();
				} else if (elem instanceof IProject) {
					project = (IProject) elem;
				} else if (elem instanceof IJavaProject) {
					project = ((IJavaProject) elem).getProject();
				}
				if (project != null)
					try {
						addNatureToProject(project, GdeProject.ID);
					} catch (CoreException e) {
						e.printStackTrace();
						error++;
					}
			}
		}
		
		if (error > 0) {
			MessageDialog.openInformation(this.getDisplay().getActiveShell(), "Enable GDE Support", "There are no projects to convert.");
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		mSelection = selection;
	}

	private static void addNatureToProject(IProject proj, String natureId) throws CoreException {
		IProjectDescription description = proj.getDescription();
		String[] prevNatures = description.getNatureIds();
		String[] newNatures = new String[prevNatures.length + 1];
		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
		newNatures[prevNatures.length] = natureId;
		description.setNatureIds(newNatures);
		proj.setDescription(description, new NullProgressMonitor());
	}

	private IProject[] getUnconvertedProjects() {
		ArrayList<IProject> unconverted = new ArrayList<IProject>();
		IProject[] projects = GdeUI.getWorkspace().getRoot().getProjects();
		for (int i = 0; i < projects.length; i++) {
			if (projects[i].isOpen() && !Gde.hasGdeProjectNature(projects[i]))
				unconverted.add(projects[i]);
		}
		return (IProject[]) unconverted.toArray(new IProject[unconverted.size()]);
	}

	private Display getDisplay() {
		Display display = Display.getCurrent();
		if (display == null)
			display = Display.getDefault();
		return display;
	}

}
